//
//  JWSentinelDrawable.m
//  KotH
//
//  Created by James Weatherley on 8/11/2009.
//  Copyright 2009 James Weatherley. All rights reserved.
//

#import "../koth.h"
#import "../wolfiphone.h"

@implementation JWSentinelManager


@synthesize sentinels;

-(id)initWithEntity:(JWSentinel*)sentinel
{
	self = [super init];
	sentinels = [[NSMutableArray alloc] initWithCapacity:MAX_SENTINELS];
	
	if(sentinel) {
		[sentinels addObject:sentinel];
	}
	
	return self;
}

-(void)dealloc
{
	[sentinels release];
	[super dealloc];
}

-(NSObject*)entity
{
	// Zeroth sentinel is the level boss.
	return [sentinels objectAtIndex:0];
}

-(void)addEntity:(NSObject*)entity
{
	// The worst it can get is MAX_SENTINELS sentinels and MAX_SENTINELS meanies.
	assert([sentinels count] < 2 * MAX_SENTINELS);
	[sentinels addObject:entity];
}

-(void)removeEntity:(NSObject*)entity
{
	JWSentinel* sentinel = (JWSentinel*)entity;
	sentinel.square.sentinel = nil;
	[sentinels removeObject:entity];
}

-(void)removeAllEntities
{
	[sentinels removeAllObjects];
}

-(void)draw
{	
	// This is somewhat inefficient, and could do with being sorted out.
	GLfloat* colour = 0;
	GLfloat* topColour = 0;
	
	for(JWSentinel* sentinel in sentinels) {
	
		if(sentinel.square.selected) {
			colour = selectedColour;
		} else {
			if(sentinel.sentinelId == 0) {
				colour = bossColour;
			} else if(sentinel.sentinelId == 666) {
				colour = meanieColour;
			} else {
				colour = sentinelColour;
			}
		}
		
		vec3_t* bodyNormals = [sentinel bodyNormals];
		vec3_t* bodyVertices = [sentinel bodyVertices];
		vec3_t* headNormals = [sentinel headNormals];
		vec3_t* headVertices = [sentinel headVertices];
		
		JWSquare* square = sentinel.square;		
			
		for(NSUInteger i = 0; i < 4; ++i) {
			int a = i;
			int b = (i + 1) % 4;
			int c = (i + 1) % 4 + 4;
			int d = i + 4;
			
			pfglBegin(GL_QUADS);
			pfglColor4f(colour[0], colour[1], colour[2], 1.0f);
			pfglNormal3f(bodyNormals[i][0], bodyNormals[i][1], bodyNormals[i][2]);
			pfglVertex3f(bodyVertices[a][0], bodyVertices[a][1], bodyVertices[a][2]);
			pfglVertex3f(bodyVertices[b][0], bodyVertices[b][1], bodyVertices[b][2]);
			pfglVertex3f(bodyVertices[c][0], bodyVertices[c][1], bodyVertices[c][2]);
			pfglVertex3f(bodyVertices[d][0], bodyVertices[d][1], bodyVertices[d][2]);
			pfglEnd();
			
			if(i == 0) {
				topColour = visorColour;
			} else {
				topColour = colour;
			}
			
			qglPushMatrix();
			{
				pfglBegin(GL_QUADS);
				pfglTranslatef(square.x + 0.5f, square.height, square.z + 0.5f);
				pfglRotatef(sentinel.theta, 0.0f, 1.0f, 0.0f);
				
				pfglColor4f(topColour[0], topColour[1], topColour[2], 1.0f);
				pfglNormal3f(headNormals[i][0], headNormals[i][1], headNormals[i][2]);
				pfglVertex3f(headVertices[a][0], headVertices[a][1], headVertices[a][2]);
				pfglVertex3f(headVertices[b][0], headVertices[b][1], headVertices[b][2]);
				pfglVertex3f(headVertices[c][0], headVertices[c][1], headVertices[c][2]);
				pfglVertex3f(headVertices[d][0], headVertices[d][1], headVertices[d][2]);
				
				pfglEnd();
			}
			qglPopMatrix();
		}
		
		qglPushMatrix();
		{
			pfglBegin(GL_QUADS);
			pfglTranslatef(square.x + 0.5f, square.height, square.z + 0.5f);
			pfglRotatef(sentinel.theta, 0.0f, 1.0f, 0.0f);
			
			pfglColor4f(colour[0], colour[1], colour[2], 1.0f);
			pfglNormal3f(headNormals[4][0], headNormals[4][1], headNormals[4][2]);
			pfglVertex3f(headVertices[3][0], headVertices[3][1], headVertices[3][2]);
			pfglVertex3f(headVertices[2][0], headVertices[2][1], headVertices[2][2]);
			pfglVertex3f(headVertices[1][0], headVertices[1][1], headVertices[1][2]);
			pfglVertex3f(headVertices[0][0], headVertices[0][1], headVertices[0][2]);
			
			pfglNormal3f(headNormals[5][0], headNormals[5][1], headNormals[5][2]);
			pfglVertex3f(headVertices[4][0], headVertices[4][1], headVertices[4][2]);
			pfglVertex3f(headVertices[5][0], headVertices[5][1], headVertices[5][2]);
			pfglVertex3f(headVertices[6][0], headVertices[6][1], headVertices[6][2]);
			pfglVertex3f(headVertices[7][0], headVertices[7][1], headVertices[7][2]);

			pfglEnd();
		}
		qglPopMatrix();
	}
}

-(void)postDraw
{
}

@end
